<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 学生管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <!--                <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">-->
        <!--                    <el-option key="1" label="广东省" value="广东省"></el-option>-->
        <!--                    <el-option key="2" label="湖南省" value="湖南省"></el-option>-->
        <!--                </el-select>-->
        <el-input v-model="teacherSearch" placeholder="学号/姓名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <!--                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>-->
        <!--        <el-table-column prop="name" label="用户名"></el-table-column>-->
        <el-table-column label="姓名">
          <template #default="scope">{{ scope.row.teacherName }}</template>
        </el-table-column>
        <el-table-column label="工号" align="center">
          <template #default="scope">{{ scope.row.teacherNum }}</template>
        </el-table-column>
        <el-table-column label="性别">
          <template #default="scope">{{ scope.row.teacherSex }}</template>
        </el-table-column>
        <el-table-column label="学院">
          <template #default="scope">{{ scope.row.teacherCollege }}</template>
        </el-table-column>
        <!--        <el-table-column label="学院">-->
        <!--          <template #default="scope">{{ scope.row.collegeName }}</template>-->
        <!--        </el-table-column>-->
        <el-table-column prop="date" label="操作">
          <template #default="scope">
            <el-button type="primary" @click="open(scope.row)" style="width: 120px">编 辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      <div class="pagination">-->
      <!--        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"-->
      <!--                       :page-size="query.pageSize" :total="pageTotal"-->
      <!--                       @current-change="handlePageChange"></el-pagination>-->
      <!--      </div>-->
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="老师信息编辑" v-model="editVisible" width="50%">
      <el-form label-width="70px">
        <el-form-item label="姓名" disabled="true">
          <el-input v-model="teacherInfo.teacherName"></el-input>
        </el-form-item>
        <el-form-item label="工号">
          <el-input v-model="teacherInfo.teacherNum"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="teacherInfo.teacherSex" disabled></el-input>
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="teacherInfo.teacherCollege"></el-input>
        </el-form-item>
      </el-form>
      <template #footer #default="scope">
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit()">确 定</el-button>
                </span>
      </template>
    </el-dialog>

    <el-dialog title="老师信息编辑" v-model="editAddVisible" width="50%">
      <el-form label-width="70px">
        <el-form-item label="姓名" disabled="true">
          <el-input v-model="teacherInfo.teacherName"></el-input>
        </el-form-item>
        <el-form-item label="工号">
          <el-input v-model="teacherInfo.teacherNum"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="teacherInfo.teacherSex"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="teacherInfo.password"></el-input>
        </el-form-item>
        <el-form-item label="学院">
          <el-input v-model="teacherInfo.teacherCollege"></el-input>
        </el-form-item>
      </el-form>
      <template #footer #default="scope">
                <span class="dialog-footer">
                    <el-button @click="editAddVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveAddEdit()">确 定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {selectTeachers, addOrUpdateTeacher, addOrUpdateStudent, selectStudents} from "../api/index";
export default {
  name: "TeacherManagement",
  data() {
    return {
      teacherSearch: '',
      tableData: [{
        id: 0,
        teacherName: '',
        teacherNum: '',
        teacherCollege: '',
        teacherSex: '',
      }],
      editVisible: false,
      teacherInfo: {
        id: 0,
        teacherName: '',
        teacherNum: '',
        teacherCollege: '',
        teacherSex: '',
        password:'',
      },
      editAddVisible:false,
    }
  },
  methods:{
    handleSearch(){
      selectTeachers({teacherNum:this.teacherSearch}).then(res => {
        if(res.code === '200'){
          this.tableData = res.data;
        }
      })
    },
    saveEdit() {
      addOrUpdateTeacher(this.teacherInfo).then(res => {
        if (res.code === '200') {
          this.$message("成功");
        }
      })
      this.handleSearch();
      this.editVisible = false;
    },
    open(row) {
      // this.studentInfo = this.tableData[row.$index];
      selectTeachers({teacherNum: row.teacherNum}).then(res => {
        if (res.code === '200') {
          this.teacherInfo = res.data[0];
        }
      })
      this.editVisible = true;
    },
    handleAdd(){
      this.editAddVisible = true;
    },
    saveAddEdit(){
      addOrUpdateTeacher(this.teacherInfo).then(res => {
        if(res.code === '200'){
          this.$message('成功');
        }
      })
      this.teacherInfo.teacherCollege = '';
      this.teacherInfo.teacherSex = '';
      this.teacherInfo.teacherName = '';
      this.teacherInfo.teacherNum = '';
      this.teacherInfo.password = '';
    }
  },
  mounted() {
    this.handleSearch();
  }

}
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>